<?php
function displayDefault($page_content, $page_title = null, $page_keywords = null, $page_description = null){//create page with default template
	$query = "SELECT template_content FROM kse_site_templates WHERE template_id='1'";
	$result = mysql_query($query);
	list($template_content) = mysql_fetch_row($result);
	
	$ksetags = array("<kse>page_title</kse>", "<kse>page_keywords</kse>", "<kse>page_description</kse>", "<kse>page_content</kse>", "<kse>baseurl</kse>");//changing kse special tags on content
	$ksevars = array($page_title, $page_keywords, $page_description, $page_content, domain_path);

	echo str_replace($ksetags, $ksevars, $template_content);
}

function displayTemplated($page_title, $page_keywords, $page_description, $page_content, $page_template_id, $page_locked){//create page with template
	$query = "SELECT template_id, template_content, template_objects, template_modules  FROM kse_site_templates WHERE template_id='". $page_template_id ."'";
	$result = mysql_query($query);

	if(mysql_num_rows($result) > 0){//check if template is exist
		if(!$page_locked){
			list($template_id, $template_content, $template_objects, $template_modules) = mysql_fetch_row($result);
			
		
			if($template_objects){
				$query = "SELECT SQL_NO_CACHE t2.object_filesystem_name, t2.object_content FROM kse_site_template_objects AS t1 LEFT JOIN kse_site_objects AS t2 ON t1.to_object_id = t2.object_id  WHERE t1.to_template_id=". $template_id .";";
				$result = mysql_query($query);
				while($row = mysql_fetch_assoc($result)){
					$template_content = str_replace("<kse>". $row["object_filesystem_name"] ."</kse>", $row["object_content"], $template_content);;
				}
			}
			
			/*genereting text for buffer*/
			$ksetags = array("<kse>page_title</kse>", "<kse>page_keywords</kse>", "<kse>page_description</kse>", "<kse>page_content</kse>", "<kse>baseurl</kse>");//changing kse special tags on content
			$ksevars = array($page_title, $page_keywords, $page_description, $page_content, domain_path);
			$site_templ_content = str_replace($ksetags, $ksevars, $template_content);
			
			/*executing code and write to buffer content */
			ob_start();
			ob_implicit_flush(0);
			eval("?>" . $site_templ_content);   
			$site_templ_content = ob_get_contents();
			ob_end_clean();
			
			/*output content*/
			echo $site_templ_content;
				
			/*eval("?>". str_replace($ksetags, $ksevars, $template_content) ."<?php");*/
		}
		else{
			return displayError(503);
		}
	}
	else{
		displayDefault($page_content, $page_title, $page_keywords, $page_description);
	}
}

function displayError($error){
	switch ($error) {
		case 404:
			header("HTTP/1.0 404 Not Found");
			displayDefault("404 Page no Found", "404");
		break;
		case 503:
			header("HTTP/1.0 503 Service Unavailable");
			displayDefault("503 Page is temporary unavailable", "503");
		break;
		case 401:
			header("HTTP/1.0 401 Unauthorized");
			displayDefault("401 Unauthorized", "401");
		break;
		default:
			echo "Error";
		break;
	}
	
}

function displayResult(){
//display some result
}

function loadObjects($object_name){
	if(objects){
		if(file_exists(objects_folder . $object_name . ".php")){
			include(objects_folder . $object_name . ".php");
		}
		else{
			echo "No such object '". $object_name ."' in ". objects_folder ." folder";
		}
	}
	else{
		echo "Objects are currently disabled";
	}
}

function LoadModule($module_name){
	if(modules){
		/*if(file_exists(modules_core_folder . $module_name . DIRSEP . "view.php") && file_exists(modules_core_folder . $module_name . DIRSEP . "model.php") && file_exists(modules_core_folder . $module_name . DIRSEP . "controller.php")){
			echo "Successfully loaded core module '". $module_name ."'";
		}
		elseif(file_exists(modules_folder . $module_name . DIRSEP . "view.php") && file_exists(modules_folder . $module_name . DIRSEP . "model.php") && file_exists(modules_core_folder . $module_name . DIRSEP . "controller.php")){
			echo "Successfully loaded module '". $module_name ."'";
		}
		else{
			echo "Failed to load module '". $module_name ."'";
		}
		include(modules_core_folder . $module_name . DIRSEP . "view.php");
		include(modules_core_folder . $module_name . DIRSEP . "model.php");
		include(modules_core_folder . $module_name . DIRSEP . "controller.php");*/
		if(file_exists(modules_folder . $module_name . DIRSEP . "functions.php")){
			//echo "Successfully loaded module '". $module_name ."'";
			include(modules_folder . $module_name . DIRSEP . "functions.php");
		}
		else{
			echo "Failed to load module '". $module_name ."'<br>
			No such file '". modules_folder . $module_name . DIRSEP . "functions.php" ."'";
		}
	}
	else{
		echo "Modules are currently disabled";
	}
}
?>
